<script>
export default {
  name: "PermissionList",
  computed: {
    /*stateBoolean(val) {
      return val === 1;
    }*/
  },
  data() {
    return {
      queryInfo: {
        keyword: '',
        currentPage: 1,
        pageSize: 8
      },
      total: 0,
      Permission: [
      ],
      listLoading: false,
      sels: [],
      editFormVisible: false,
      editForm: {
        id: '',
        name: '',
        intro: '',
        createTime: '',
        updateTime: '',
        path: '',
        state: '',
        manager: {
          id: '',
          username: '',
        },
        parent: {
          id: '',
          name: ''
        }
      },
      editLoading: false,
      departmentTree: [],
      managerList: [],
    }
  },
  methods: {
    getPermission() {
      this.$http.post("/permission", this.queryInfo).then(res => {
        // console.log(res)
        if (res.data.success) {
          this.Permission = res.data.data.list
          this.total = res.data.data.total
          /*this.$message({
            message: res.data.msg,
            type: "success",
            showClose: true
          })*/
        } else {
          this.$message({
            message: res.data.msg,
            type: "error",
            showClose: true
          })
        }
      }).catch(err => {
        this.$message({
          message: "请求异常",
          type: "error",
          showClose: true
        })
      }).finally(() => {
        this.listLoading = false;
      })
    },
    queryDepartment() {
      this.getPermission();
    },
    handleAdd() {
      this.editFormVisible = true;
      this.editForm = {
        id: '',
        name: '',
        intro: '',
        createTime: '',
        updateTime: '',
        path: '',
        state: '',
        manager: {
          id: '',
          username: '',
        },
        parent: {
          id: '',
          name: ''
        }
      };
    },
    handleSizeChange(val) {
      this.queryInfo.pageSize = val;
      this.getPermission();
    },
    selsChange(sels) {
      this.sels = sels
    },
    batchRemove() {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let ids = this.sels.map(item => item.id);
        // console.log(ids)
        this.$http.patch("/permission", ids).then(res => {
          if (res.data.success) {
            this.$message({
              message: res.data.msg,
              type: "success",
              showClose: true
            })
            this.getPermission();
          }
        }).catch(err => {
          this.$message({
            message: "请求异常",
            type: "error",
            showClose: true
          })
        }).finally(() => {
          this.listLoading = false;
        })
      }).catch(() => {

      });
    },
    handleCurrentChange(val) {
      this.queryInfo.currentPage = val;
      this.getPermission();
    },
    handleEdit(index, row) {
      console.log(row)
      console.log("row.id = " + row.id)
      this.editFormVisible = true;
      this.queryEmployeeList();
      this.queryDepartmentTree(row.id);
      console.log(row)
      // this.editForm = row;
      /*
        这行代码的作用是创建一个新的对象，并将 `row` 对象的所有属性复制到新的对象中，
        然后将新的对象赋值给 `this.editForm`。
        在 JavaScript 中，`Object.assign(target, ...sources)`
        方法用于将所有可枚举的自有属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。
        在这个例子中，`Object.assign({}, row)` 创建了一个新的对象，并将 `row` 对象的所有属性复制到新的对象中。
        然后，这个新的对象被赋值给 `this.editForm`。

        这样做的目的通常是为了避免直接修改原始数据 `row`，因为在 JavaScript 中，对象是通过引用传递的，
        如果你直接将 `row` 赋值给 `this.editForm`，
        那么当你修改 `this.editForm` 的时候，`row` 也会被修改。
        通过使用 `Object.assign` 创建一个新的对象，你可以避免这个问题。
      * */
      this.editForm = Object.assign({}, row);
      console.log(this.editForm)
      if (!this.row.manager) {
        this.editForm.manager = {
          id: null,
          username: ''
        }
      }
      if (!this.row.parent) {
        this.editForm.parent = {
          id: null,
          name: ''
        }
      }
    },
    handleDel(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/permission/" + row.id).then(res => {
          if (res.data.success) {
            this.$message({
              message: res.data.msg,
              type: "success",
              showClose: true
            })
            this.getPermission();
          }
        }).catch(err => {
          this.$message({
            message: "请求异常",
            type: "error",
            showClose: true
          })
        }).finally(() => {
          this.listLoading = false;
        })
      }).catch(() => {

      });
    },
    editSubmit() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.editLoading = true;
          // console.log("parent.id = ")
          // console.log(this.editForm.parent.id)

          if (this.editForm.parent.id.length === 0) {
            this.editForm.parent.id = null;
          } else {
            this.editForm.parent.id = this.editForm.parent.id[this.editForm.parent.id.length - 1];
          }
          this.$http.put("/permission", this.editForm).then(res => {
            if (res.data.success) {
              this.editFormVisible = false;
              this.$message({
                message: res.data.msg,
                type: "success",
                showClose: true
              })
              this.getPermission();
            }
          }).catch(err => {
            this.$message({
              message: "请求异常",
              type: "error",
              showClose: true
            })
          }).finally(() => {
            this.editLoading = false;
          })
        }
      })
    },
    queryDepartmentTree(id) {
      this.$http.get("/permission/parents/" + id).then(res =>{
        if (res.data.success) {
          this.departmentTree = res.data.data;
        }
      }).catch(error => {
        this.$message({
          message: "父级部门列表获取失败",
          type: "error",
          showClose: true
        })
      })
    },
    queryEmployeeList() {
      this.$http.get("/employee").then(res => {
        if (res.data.success) {
          this.managerList = res.data.data;
        }
      }).catch(error => {
        this.$message({
          message: "部门经理列表获取失败",
          type: "error",
          showClose: true
        })
      })
    }
  },
  mounted() {
    this.getPermission()
  }
}
</script>

<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="queryInfo.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryDepartment">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="Permission" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              max-height="460px"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="权限名称" width="200" sortable/>
      <el-table-column prop="desc" label="权限介绍" width="200" sortable/>
      <el-table-column prop="url" label="权限地址" width="140" sortable/>
      <el-table-column prop="sn" label="权限唯一标识" width="300" sortable/>
      <el-table-column prop="parent.name" label="父级权限" width="200" sortable/>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.currentPage"
          :page-sizes="[3, 5, 8, 10, 20]"
          :page-size="8"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editFormVisible">
<!--      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">-->
      <el-form :model="editForm" label-width="80px" ref="editForm">
        <el-form-item label="部门名称">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门简介" prop="intro">
          <el-input v-model="editForm.intro" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门经理" prop="manager.username">
          <el-select v-model="editForm.manager.id" placeholder="请选择">
            <el-option
                v-for="item in managerList"
                :key="item.id"
                :label="item.username"
                :value="item.id">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.email }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="父级部门" prop="parent.name">
          <el-cascader
              :options="departmentTree"
              :props="{
              checkStrictly: true,
              value: 'id',
              label: 'name'
            }"
              v-model="editForm.parent.id"
              clearable></el-cascader>
        </el-form-item>
        <el-form-item label="部门状态" prop="state">
<!--          <el-input v-model="editForm.state" auto-complete="off"></el-input>-->
          <el-switch
              v-model="editForm.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="启用"
              inactive-text="禁用"
              :active-value="1"
              :inactive-value="0">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<style scoped>

</style>
